using System;
using System.Linq.Expressions;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp.Data;
using Volo.Abp.Application.Dtos;
using Joy.Erp.BasicArchives.Permissions;
using Joy.Erp.BasicArchives.Core.Filters;

namespace Joy.Erp.BasicArchives.ProductionUnit
{
    [Authorize]
    public class WorkCenterAppService : BasicArchivesAppService, IWorkCenterAppService
    {
        protected IWorkCenterRepository WorkCenterRepository => LazyServiceProvider.LazyGetRequiredService<IWorkCenterRepository>();
        protected WorkCenterManager WorkCenterManager => LazyServiceProvider.LazyGetRequiredService<WorkCenterManager>();

        [Authorize(BasicArchivesPermissions.WorkCenter.Query)]
        public async Task<WorkCenterDto> GetAsync(Guid id)
        {
            return ObjectMapper.Map<WorkCenter, WorkCenterDto>(
                await WorkCenterRepository.GetAsync(id, true)
            );
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Query)]
        public async Task<WorkCenterDto> FindAsync(string filter)
        {
            var entity = await WorkCenterRepository.FindAsync(x => x.Code == filter);
            return ObjectMapper.Map<WorkCenter, WorkCenterDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Query)]
        public async Task<WorkCenterDto> FindBySourceKeyAsync(string sourceKey, string sourceType)
        {
            var entity = await WorkCenterRepository.FindAsync(x => x.SourceKey == sourceKey && x.SourceType == sourceType);
            return ObjectMapper.Map<WorkCenter, WorkCenterDto>(entity);
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Query)]
        public async Task<PagedResultDto<WorkCenterDto>> GetListAsync(FilterRequestDto input)
        {
            var totalCount = await WorkCenterRepository.GetCountAsync(input.Filter, true);
            var list = await WorkCenterRepository.GetPagedListAsync(input.Filter, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<WorkCenterDto>(
                totalCount,
                ObjectMapper.Map<List<WorkCenter>, List<WorkCenterDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Query)]
        public async Task<PagedResultDto<WorkCenterDto>> QueryAsync(FilterGroupRequestDto input)
        {
            Expression<Func<WorkCenter, bool>> expression = null;
            if (input != null && input.FilterGroup != null)
            {
                expression = FilterConverter.Convert<WorkCenter>(input.FilterGroup);
            }

            var totalCount = await WorkCenterRepository.GetCountAsync(expression, true);
            var list = await WorkCenterRepository.GetPagedListAsync(expression, input.SkipCount, input.MaxResultCount, input.Sorting, true);

            return new PagedResultDto<WorkCenterDto>(
                totalCount,
                ObjectMapper.Map<List<WorkCenter>, List<WorkCenterDto>>(list));
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Create)]
        public async Task<WorkCenterDto> CreateAsync(WorkCenterCreateDto input)
        {
            var entity = ObjectMapper.Map<WorkCenterCreateDto, WorkCenter>(input);

            await WorkCenterManager.CreateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Update)]
        public async Task<WorkCenterDto> UpdateAsync(Guid id, WorkCenterUpdateDto input)
        {
            var entity = await WorkCenterRepository.GetAsync(id);
            entity.SetConcurrencyStampIfNotNull(input.ConcurrencyStamp);

            ObjectMapper.Map(input, entity);

            await WorkCenterManager.UpdateAsync(entity, true);
            return await GetAsync(entity.Id);
        }

        [Authorize(BasicArchivesPermissions.WorkCenter.Delete)]
        public async Task DeleteAsync(Guid id)
        {
            await WorkCenterManager.DeleteAsync(id);
        }
    }
}